﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Medicalsystem.MCP.FormDesigner.Common;

namespace Medicalsystem.MCP.FormDesigner
{
    /// <summary>
    /// 容器管理者-用于无用户界面控件
    /// </summary>
    public class ComContainerHandler : ContainerHandler
    {
        /// <summary>
        /// 容器管理者
        /// </summary>
        /// <param name="container">控件容器</param>
        /// <param name="modelDesign">关联的模板管理者</param>
        public ComContainerHandler(System.Windows.Forms.Control container, ModelDesign modelDesign)
            : base(container, null, modelDesign)
        {
            Container.MouseDown += new MouseEventHandler(Container_MouseDown);
            Container.MouseMove += new MouseEventHandler(Container_MouseMove);
            Container.MouseUp += new MouseEventHandler(Container_MouseUp);
        }

        private bool _isMouseDown = false;

        #region 鼠标事件

        private void Container_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                _isMouseDown = true;

                this.Container.Capture = true;
                Cursor.Clip = this.Container.RectangleToScreen(this.Container.ClientRectangle);
                _mouseRect = new Rectangle(e.X, e.Y, 0, 0);
            }
            catch (Exception ex)
            {
                TryCustomException.TransactException(ex);
            }
        }

        private void Container_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (_isMouseDown)
                {
                    ResizeToRectangle(e.Location, FrameStyle.Dashed);
                }
            }
            catch (Exception ex)
            {
                TryCustomException.TransactException(ex);
            }
        }

        /// <summary>
        /// 选取（将所有选取项添加入选取列表，如果选取项为零需要重置某些设计器界面控件显示）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Container_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                _isMouseDown = false;
                this.Container.Capture = false;
                Cursor.Clip = Rectangle.Empty;

                //规范化选取区域
                if (_mouseRect.Width < 0)
                {
                    _mouseRect.X += _mouseRect.Width;
                    _mouseRect.Width = Math.Abs(_mouseRect.Width);
                }

                if (_mouseRect.Height < 0)
                {
                    _mouseRect.Y += _mouseRect.Height;
                    _mouseRect.Height = Math.Abs(_mouseRect.Height);
                }

                //begion
                this.Container.SuspendLayout();

                foreach (CtrlMngDesign ctrlMngDesign in this.ModelDesign.Controls.Ctrls)
                {
                    //限制选取的是同一容器控件
                    if ((ctrlMngDesign.ParentContainer.Container == Container) && _mouseRect.IntersectsWith(new Rectangle(ctrlMngDesign.Owner.Location, ctrlMngDesign.Owner.Size)))
                    {
                        this.ModelDesign.SelectListAppend(ctrlMngDesign);
                    }
                }

                this.ModelDesign.SelectListAppendEnd();
                _mouseRect = Rectangle.Empty;

                //如果选取项为零，需要空置控件属性设置窗口
                if (this.ModelDesign.SelectList.Count == 0)
                {
                    SingleList.Instance.OnMessageEvent(null, MessageType.ModelFocus);
                }

                //end
                this.Container.ResumeLayout(false);
                this.Container.PerformLayout();
                this.Container.Refresh();
            }
            catch (Exception ex)
            {
                TryCustomException.TransactException(ex);
            }
        }

        #endregion 鼠标事件
    }
}